package com.hongkzh.www.look.enterprise.presenter;

import com.hongkzh.www.look.model.bean.AttenedBean;
import com.hongkzh.www.look.model.bean.HistoryBean;
import com.hongkzh.www.other.api.UrlWrapper;
import com.hongkzh.www.other.okhttp.CallBackUtil;
import com.hongkzh.www.other.okhttp.OkhttpUtil;
import com.hongkzh.www.look.view.iview.HistoryIView;
import com.hongkzh.www.presenter.BasePresenter;

import okhttp3.Call;

import static com.hongkzh.www.other.api.Api.HISTORYLIST;
import static com.hongkzh.www.other.api.Api.HISTORYDELETE;

/**
 * Created by as on 2018/4/8.
 * 历史
 */

public class HistoryPresenter extends BasePresenter<HistoryIView> {

    private boolean isLastPage;

    @Override
    public void attachView(HistoryIView iView) {
        super.attachView(iView);
    }


    /**
     * 请求 观看历史列表
     * @param loginUid
     * @param pageNum
     */
    public void getMyHistoryList(String loginUid,String pageNum){
        UrlWrapper urlWrapper= UrlWrapper.getInstance();
        urlWrapper.addParameter("loginUid",loginUid);
        urlWrapper.addParameter("pageNumber",pageNum);
        String mHistoryUrl= HISTORYLIST +urlWrapper.getRequestURL();

        OkhttpUtil.okHttpPost(mHistoryUrl, new CallBackUtil<HistoryBean>() {
            @Override
            public void onFailure(Call call, Exception e) {

            }

            @Override
            public void onResponse(HistoryBean response) {
                isLastPage=response.getData().isLastPage();
                if(isLastPage)getIView().onLastPage(isLastPage);
                getIView().onShowHistoryList(response);
            }
        });
    }

    /**
     *  删除历史观看记录
     * @param id
     */
    public void getHistoryDelete(String id) {
        UrlWrapper urlWrapper = UrlWrapper.getInstance();
        urlWrapper.addParameter("id",id);
        String hisDetUrl=HISTORYDELETE+urlWrapper.getRequestURL();
        OkhttpUtil.okHttpPost(hisDetUrl, new CallBackUtil<AttenedBean>() {
            @Override
            public void onFailure(Call call, Exception e) {

            }

            @Override
            public void onResponse(AttenedBean response) {
                int code=response.getCode();
                if(code==0)
                    getIView().deleteHistoryItem(response);
            }
        });
    }

    @Override
    public HistoryIView getIView() {
        return super.getIView();
    }


    @Override
    public void detachView() {
        super.detachView();
    }

}
